﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ExtAspNet;
using System.Data.OleDb;
using System.Data;
using Ylh.BLL;

public partial class Module_StaffManger_StaffImport :PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button2.OnClientClick = Window1.GetSaveStateReference(FilePath.ClientID) + Window1.GetShowReference("UpLoadFile.aspx", "文件上传");

       //Button4.OnClientClick = ActiveWindow.GetHideReference();
    }
    protected void BtnDownTemp_Click(object sender, EventArgs e)
    {
        PageContext.RegisterStartupScript("window.open('DownTemp.aspx','newwindow','width=400px,height=200px')");
    }

    protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
    {
        Grid1.PageIndex = e.NewPageIndex;
    }

    protected void BtnView_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(FilePath.Text))
        {
            Alert.Show("请先选择文件");
            return;
        }
        DataSet ds = ExcelDataSource(Server.MapPath(FilePath.Text), "Sheet1$");

        DataTable dt = ds.Tables[0];
        Grid1.DataSource = dt;
        Grid1.DataBind();
    }

    public DataSet ExcelDataSource(string filepath, string sheetname)
    {
        string strConn;
        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
        OleDbConnection conn = new OleDbConnection(strConn);
        conn.Open();
        OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "]", strConn);
        DataSet ds = new DataSet();
        oada.Fill(ds);
        conn.Close();
        return ds;
    }
    protected void BtnConfirm_Click(object sender, EventArgs e)
    {
        DataSet ds = ExcelDataSource(Server.MapPath(FilePath.Text), "Sheet1$");
        DataTable dt = ds.Tables[0];
        try
        {
StaffBLL sbll = new StaffBLL(DbConnectionStr);

        sbll.improtDataBase(dt, Request.QueryString["DeID"].ToString());
        Alert.Show("数据导入成功");
        PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        catch (Exception)
        {
            Alert.Show("数据导入失败,请检查数据模板是否按要求填写");
        }

        



    }
}